// pages/diancandetail/diancandetail.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order:{
      ordernum:'',
      pstype: '',
      food: [],
      zkprice: '',
      numzong: '',
      prices: '',
      ordertype: ''
    },
    items: [
      { name: '0', value: '订多了', checked: 'true' },
      { name: '1', value: '点错了重新点'},
      { name: '2', value: '临时有事不吃了' },
      { name: '3', value: '其他' },
    ],
    refundshow:true,
    textareashow:true,
    stats: ['待付款', '已付款', '配送中', '订单完成', '退款完成订单关闭', '退款中', '订单关闭', '待结单', '退款中', '待领取']
  },
  radioChange: function (e) {
    if (e.detail.value==3){
      this.setData({
        textareashow:false        
      })
    }else{
      this.setData({
        textareashow:true
      })
    }
  },
  refund1:function(e){
    this.setData({
      refundshow: !this.data.refundshow 
    })
  },
  formSubmit: function (e) {
    var that=this;
    var str='';
    if (e.detail.value=='3'){
      str = e.detail.value.reason
    }else{
      that.data.items.forEach(function(item){
        if (item.name == e.detail.value.radio){
          str=item.value;
        }
      })
    }
    console.log(that.data.order.id,str);
    wx.request({
      url: app.globalData.directory + 'api/v2/order/refund',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      method:'POST',
      data:{
        id:that.data.order.id,
        refund_reason:str
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          if(res.data.msg=='success'){
            wx.showToast({
              title: '退款请求已提交',
              icon: 'success',
              duration: 2000,
            })
            setTimeout(function(){
              wx.navigateTo({
                url: '../dingdan/dingdan?id=2',
              })
            },2000);
            
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'success',
              duration: 2000,
            })
          }
        } else {
          wx.showToast({
            title: '请求失败,请重试',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  /*取消订单 */
  refund: function (e) {
    var that = this;
    wx.request({
      url: app.globalData.directory + 'api/v2/order/close',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      method: "POST",
      data: {
        id: e.currentTarget.dataset.sn
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          if (res.data.msg == 'success') {
            wx.showToast({
              title: '取消订单成功',
              icon: 'successs',
              duration: 2000,
            })
            setTimeout(function () {
              wx.navigateTo({
                url: '../dingdan/dingdan?id=1',
              })
            }, 2000);
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000,
            })
          }
        } else {
          wx.showToast({
            title: '请求失败,请重试',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  /*付款 */
  buy: function (e) {
    var that = this;
    wx.request({
      url: app.globalData.directory + 'api/v2/pay/order_sn/' + e.currentTarget.dataset.sn,
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          console.log(res.data.data);
          wx.requestPayment({
            'timeStamp': res.data.data.timestamp,
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': res.data.data.signType,
            'paySign': res.data.data.paySign,
            'success': function (res) {
              setTimeout(function () {
                wx.navigateTo({
                  url: '../dingdan/dingdan?id=2',
                })
              }, 2000);
            },
            'fail': function (res) {
              wx.showToast({
                title: res.errMsg,
                icon: 'none',
                duration: 2000,
              })
            }
          })
          // setTimeout(function () {
          //   wx.navigateTo({
          //     url: '../dingdan/dingdan?id=1',
          //   })
          // }, 2000);
        } else {
          wx.showToast({
            title: '请求失败,请重试',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  /*评价 */
  pj: function (e) {
    wx.navigateTo({
      url: '../to-evaluate/to-evaluate?id=' + e.currentTarget.dataset.id,
    })
  },
  /*再来一单 */
  order1: function () {
    wx.switchTab({
      url: '../order/order'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: app.globalData.directory + 'api/v2/order/getinfo/' + options.id,
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          that.setData({
            order: res.data.data,
          })
        } else {
          wx.showToast({
            title: '请求失败,请重试',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})